 package cn.edu.ujn.finalwork.service;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.edu.ujn.finalwork.dao.BaseMapper;
import cn.edu.ujn.finalwork.dao.User;
import cn.edu.ujn.finalwork.dao.UserMapper;
import cn.edu.ujn.finalwork.util.MD5Encoder;
@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    UserMapper userMapper;
    
    @Autowired
    BaseMapper baseMapper;
    @Override
    public int insert(User user) {
        int insert = userMapper.insert(user);
        return insert;
    }

    @Override
    public int delectByPrimaryKey(int id) {
        int deleteByPrimaryKey = userMapper.deleteByPrimaryKey(id);
        return deleteByPrimaryKey;
    }

    @Override
    public int updateByPrimaryKey(User user) {
       int updateByPrimaryKey = userMapper.updateByPrimaryKey(user);
       return updateByPrimaryKey;
    }

    @Override
    public User selectByCode(String userCode) {
        User selectByCode = userMapper.selectByCode(userCode);
        return selectByCode;
    }
    
    @Override
    public User selectByPrimaryKey(int id) {
        User k = userMapper.selectByPrimaryKey(id);
        return k;
    }
    
    @Override
    public int insertSelective(User row) {
        String salt="123456";
        String result = null;
        String password=row.getUserPassword();
        MD5Encoder pe = new MD5Encoder();
        try {
            result = pe.geMd5(salt, password);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        row.setUserPassword(result);
        int insertSelective = userMapper.insertSelective(row);
        return insertSelective;
        
    }
    @Override
    public int updateByPrimaryKeySelective(User row) {
        int up = userMapper.updateByPrimaryKeySelective(row);
        return up;
        
    }

}
